﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TorneoLoL
{
    public partial class FormTop : Form
    {
        public FormTop()
        {
            InitializeComponent();
        }


        Conexion conexion = new Conexion();
        DataSet ds = new DataSet();
        String ConsultaSQL;

        private void FormTop_Load( object sender, EventArgs e )
        {
            groupBox1.Text = "MAYOR PROMEDIO DE KILLS";
            groupBox2.Text = "MENOR PROMEDIO DE DEATHS";
            groupBox3.Text = "MAYOR PROMEDIO DE ASISTENCIAS";
            groupBox4.Text = "MAYOR PROMEDIO DE MINIONS";
            cargargrillaMVPKTeam( ref grdBJKXE );
            cargargrillaMVPATeams( ref grdBJAXE );
            cargargrillaMVPCSTeams( ref grdBJCSXE );
            inicializarGroupBox( 1 );
            inicializarGroupBox( 2 );
            inicializarGroupBox( 3 );
            inicializarGroupBox( 4 );
        }


        void inicializarGroupBox( int aux )
        {
            float[] podio = new float[3];
            podio[0] = 0;
            podio[1] = 0;
            podio[2] = 0;

            int[] ids = new int[3];
            ids[0] = 0;
            ids[1] = 0;
            ids[2] = 0;
            try
            {

                ConsultaSQL = "select * from jugadores";
                conexion.consultarDataSet( ref ds, "jugadores", ConsultaSQL );

                int cant = ds.Tables["Jugadores"].Rows.Count;

                ds.Tables["jugadores"].Clear();

                switch(aux)
                {
                    case 1:
                        for(int a = 1 ; a <= cant ; a++)
                        {
                            float partidasJugadas = CuantasPartidas( a );

                            if(partidasJugadas > 0)
                            {
                                ConsultaSQL = "select sum(DJ_K) as killes from DesempeñoDeJugadorXEncuentro where DJ_IdJugador = " + a + " group by DJ_IdJugador";
                                conexion.consultarDataSet( ref ds, "Asesinos", ConsultaSQL );

                                string kills = ds.Tables["Asesinos"].Rows[0]["killes"].ToString();

                                ds.Tables["Asesinos"].Clear();

                                float total = Convert.ToInt32( kills ) / partidasJugadas;

                                ArmarPodio( podio, total, ids, a );
                            }
                        }
                        textBox4.Text = Convert.ToString( podio[0] );
                        textBox5.Text = Convert.ToString( podio[1] );
                        textBox6.Text = Convert.ToString( podio[2] );

                        textBox1.Text = EncontrarNick( ids[0] );
                        textBox2.Text = EncontrarNick( ids[1] );
                        textBox3.Text = EncontrarNick( ids[2] );
                        break;

                    case 2:
                        for(int a = 1 ; a <= cant ; a++)
                        {
                            float partidasJugadas = CuantasPartidas( a );

                            if(partidasJugadas > 0)
                            {
                                ConsultaSQL = "select sum(DJ_D) as deads from DesempeñoDeJugadorXEncuentro where DJ_IdJugador = " + a + " group by DJ_IdJugador";
                                conexion.consultarDataSet( ref ds, "Muertes", ConsultaSQL );

                                string kills = ds.Tables["Muertes"].Rows[0]["deads"].ToString();

                                ds.Tables["Muertes"].Clear();

                                float total = Convert.ToInt32( kills ) / partidasJugadas;

                                ArmarPodio( podio, total, ids, a );
                            }
                        }
                        textBox7.Text = Convert.ToString( podio[0] );
                        textBox8.Text = Convert.ToString( podio[1] );
                        textBox9.Text = Convert.ToString( podio[2] );

                        textBox10.Text = EncontrarNick( ids[0] );
                        textBox11.Text = EncontrarNick( ids[1] );
                        textBox12.Text = EncontrarNick( ids[2] );
                        break;

                    case 3:
                        for(int a = 1 ; a <= cant ; a++)
                        {
                            float partidasJugadas = CuantasPartidas( a );

                            if(partidasJugadas > 0)
                            {
                                ConsultaSQL = "select sum(DJ_A) as assit from DesempeñoDeJugadorXEncuentro where DJ_IdJugador = " + a + " group by DJ_IdJugador";
                                conexion.consultarDataSet( ref ds, "Asistencias", ConsultaSQL );

                                string kills = ds.Tables["Asistencias"].Rows[0]["assit"].ToString();

                                ds.Tables["Asistencias"].Clear();

                                float total = Convert.ToInt32( kills ) / partidasJugadas;

                                ArmarPodio( podio, total, ids, a );
                            }
                        }
                        textBox15.Text = Convert.ToString( podio[0] );
                        textBox14.Text = Convert.ToString( podio[1] );
                        textBox13.Text = Convert.ToString( podio[2] );

                        textBox18.Text = EncontrarNick( ids[0] );
                        textBox17.Text = EncontrarNick( ids[1] );
                        textBox16.Text = EncontrarNick( ids[2] );
                        break;

                    case 4:
                        for(int a = 1 ; a <= cant ; a++)
                        {
                            float partidasJugadas = CuantasPartidas( a );

                            if(partidasJugadas > 0)
                            {
                                ConsultaSQL = "select sum(DJ_CS) as farm from DesempeñoDeJugadorXEncuentro where DJ_IdJugador = " + a + " group by DJ_IdJugador";
                                conexion.consultarDataSet( ref ds, "Minions", ConsultaSQL );

                                string kills = ds.Tables["Minions"].Rows[0]["farm"].ToString();

                                ds.Tables["Minions"].Clear();

                                float total = Convert.ToInt32( kills ) / partidasJugadas;

                                ArmarPodio( podio, total, ids, a );
                            }
                        }
                        textBox21.Text = Convert.ToString( podio[0] );
                        textBox20.Text = Convert.ToString( podio[1] );
                        textBox19.Text = Convert.ToString( podio[2] );

                        textBox24.Text = EncontrarNick( ids[0] );
                        textBox23.Text = EncontrarNick( ids[1] );
                        textBox22.Text = EncontrarNick( ids[2] );
                        break;
                }
            }
            catch(System.Exception err)
            {
                MessageBox.Show( err.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
            }
        }


        float CuantasPartidas( int id )
        {
            try
            {
                ConsultaSQL = "select * from DesempeñoDeJugadorXEncuentro where DJ_IdJugador = " + id;
                conexion.consultarDataSet( ref ds, "jugadores", ConsultaSQL );

                float retorno = ds.Tables["Jugadores"].Rows.Count;
                ds.Tables["jugadores"].Clear();

                return retorno;
            }
            catch(System.Exception err)
            {
                MessageBox.Show( err.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
                return -1;
            }
        }

        void ArmarPodio( float[] v, float nuevo, int[] ids, int id )
        {
            if(v[0] < nuevo)
            {
                v[2] = v[1];
                ids[2] = ids[1];


                v[1] = v[0];
                ids[1] = ids[0];

                v[0] = nuevo;
                ids[0] = id;

                return;
            }
            else if(v[1] < nuevo)
            {
                v[2] = v[1];
                ids[2] = ids[1];

                v[1] = nuevo;

                ids[1] = id;


                return;
            }
            else if(v[2] < nuevo)
            {
                v[2] = nuevo;

                ids[2] = id;

            }
        }

        string EncontrarNick( int id )
        {
            try
            {
                ConsultaSQL = "select J_Nick as [nick] from jugadores where J_Id = " + id;
                conexion.consultarDataSet( ref ds, "buscar", ConsultaSQL );

                string nick = ds.Tables["buscar"].Rows[0]["nick"].ToString();

                ds.Tables["buscar"].Clear();

                return nick;
            }
            catch(System.IndexOutOfRangeException)
            {
                return String.Empty;
            }
            catch(System.Exception err)
            {
                MessageBox.Show( err.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
                return String.Empty;
            }
        }


        void cargargrillaMVPKTeam( ref DataGridView grid )
        {
            try
            {
                string aux = " MAX(J_k) as [Mejor Abatidor]";

                string consulta = "select " + aux + ", T_NombreTeam as 'Equipo' from Jugadores inner join Teams on T_IdTeam = J_IdTeam group by T_NombreTeam";
                conexion.consultarDataSet( ref ds, "MVPKTeams", consulta );
                grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                grid.DataSource = ds.Tables["MVPKTeams"];
            }
            catch(System.Exception err)
            {
                MessageBox.Show( err.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
            }
        }
        void cargargrillaMVPATeams( ref DataGridView grid )
        {
            try
            {
                string aux = " MAX(J_A) as 'Mejor Soporte'";

                string consulta = "select " + aux + ", T_NombreTeam as 'Equipo' from Jugadores inner join Teams on T_IdTeam = J_IdTeam group by T_NombreTeam";
                conexion.consultarDataSet( ref ds, "MVPATeams", consulta );
                grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                grid.DataSource = ds.Tables["MVPATeams"];
            }
            catch(System.Exception err)
            {
                MessageBox.Show( err.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
            }
        }
        void cargargrillaMVPCSTeams( ref DataGridView grid )
        {
            try
            {
                string aux = "MAX(J_CS) as 'Mejor Abrecaminos'";

                string consulta = "select " + aux + ", T_NombreTeam as 'Equipo' from Jugadores inner join Teams on T_IdTeam = J_IdTeam group by T_NombreTeam";
                conexion.consultarDataSet( ref ds, "MVPCSTeams", consulta );
                grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                grid.DataSource = ds.Tables["MVPCSTeams"];
            }
            catch(System.Exception err)
            {
                MessageBox.Show( err.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
            }
        }
    }
}
